﻿using System;
using System.Data;
using DataAccess;
using EntitiesTM;

namespace Business
{
    public class BuActivity
    {
        public static DataTable selActivityByProjectID(int projectID, string companyid)
        {
            DaActivity activity = new DaActivity();
            DataTable tmpActivity = activity.selActivityByProjectID2(projectID, companyid);
            int numOfRow = tmpActivity.Rows.Count;
            if (numOfRow > 0)
            {
                // Duyet cac hang cua datatable
                for (int i = 0; i < numOfRow; i++)
                {
                    int minute = Int32.Parse(tmpActivity.Rows[i]["Minute"].ToString());
                    if (minute >= 60)
                    {
                        int addHour = minute / 60;
                        minute -= addHour * 60;
                        tmpActivity.Rows[i]["Minute"] = minute;
                        tmpActivity.Rows[i]["Hour"] = Int32.Parse(tmpActivity.Rows[i]["Hour"].ToString()) + addHour;
                    }
                }
            }
            return tmpActivity;
        }
        public static DataTable selActivityByProjectID2(int projectID, string companyid)
        {
            DaActivity activity = new DaActivity();
            DataTable tmpActivity = activity.selActivityByProjectID2(projectID, companyid);
            int numOfRow = tmpActivity.Rows.Count;
            if (numOfRow > 0)
            {
                // Duyet cac hang cua datatable
                for (int i = 0; i < numOfRow; i++)
                {
                    int minute = Int32.Parse(tmpActivity.Rows[i]["Minute"].ToString());
                    if (minute >= 60)
                    {
                        int addHour = minute / 60;
                        minute -= addHour*60;
                        tmpActivity.Rows[i]["Minute"] = minute;
                        tmpActivity.Rows[i]["Hour"] = Int32.Parse(tmpActivity.Rows[i]["Hour"].ToString()) + addHour;
                    }
                }
            }
            return tmpActivity;
        }

        public static DataTable selTotalTime(int projectID, string companyid)
        {
            DaActivity activity = new DaActivity();
            return activity.selTotalTime(projectID, companyid);
        }

        public static DataTable selActivityByID(int activityID, string companyid)
        {
            DaActivity activity = new DaActivity();
            return activity.selActivityByID(activityID, companyid);
        }
        public static DataTable selActivityByID2(int activityID, string companyid)
        {
            DaActivity activity = new DaActivity();
            return activity.selActivityByID2(activityID, companyid);
        }
        public static void updActivity(EnActivity activity, string companyid)
        {
            DaActivity daActivity = new DaActivity();
            daActivity.updActivity(activity, companyid);
        }

        public static void updActivity2(EnActivity activity, string companyid)
        {
            DaActivity daActivity = new DaActivity();
            daActivity.updActivity2(activity, companyid);
        }

        public static void delActivity(int activityID, string companyid)
        {
            DaActivity daActivity = new DaActivity();
            daActivity.delSAActivity(activityID, companyid);
            daActivity.delActivity(activityID, companyid);
        }

        public static DataTable selReportTimeActivity(int activityID, string companyid)
        {
            DaActivity activity = new DaActivity();
            return activity.selReportTimeActivity(activityID, companyid);
        }

        public static DataTable selReportTimeActivityPerUser(int activityID, string companyid)
        {
            DaActivity activity = new DaActivity();
            //return activity.selReportTimeActivityPerUser(activityID);

            DataTable data = activity.selReportTimeActivityPerUser(activityID, companyid);
            //int dsd = data.Rows.Count;
            DataTable tmpTable = data.Clone();
            //int dsd = tmpTable.Rows.Count;
            DataColumn column = new DataColumn();
            column = new DataColumn("Time", typeof(String));
            tmpTable.Columns.Add(column);

            foreach (DataRow row in data.Rows)
            {
                tmpTable.ImportRow(row);
            }

            int numOfRow = tmpTable.Rows.Count;
            if (numOfRow > 0)
            {
                // Duyet cac hang cua datatable
                for (int i = 0; i < numOfRow; i++)
                {
                    int minute = Int32.Parse(tmpTable.Rows[i]["Minute"].ToString());
                    if (minute >= 60)
                    {
                        int addHour = minute / 60;
                        minute -= addHour * 60;
                        tmpTable.Rows[i]["Minute"] = minute;
                        tmpTable.Rows[i]["Hour"] = Int32.Parse(tmpTable.Rows[i]["Hour"].ToString()) + addHour;
                    }
                    tmpTable.Rows[i]["Time"] = tmpTable.Rows[i]["Hour"] + ":" + tmpTable.Rows[i]["Minute"];
                }
            }
            return tmpTable;
        }
    }
}
